package com.tofix.kidmall.controller;


import com.tofix.kidmall.common.CommonPage;
import com.tofix.kidmall.common.CommonResult;
import com.tofix.kidmall.entity.Cart;
import com.tofix.kidmall.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

/**
 * restful API接口
 * @author wu
 * @description CartController 购物车
 * @date 2020/6/3
 **/


@RestController()
@Api(tags = "CartController api")
@RequestMapping("/cart")

public class CartController {
    private static final Logger logger = (Logger) LoggerFactory.getLogger(CartController.class);

    @Autowired
    CartService cartService;

    @ApiOperation("根据id获取购物车")
    @GetMapping("/{id}")
    CommonResult<Cart> getById(@PathVariable String id) {
        return CommonResult.success(cartService.getById(id).orElseGet(Cart::new));
    }
    @ApiOperation("更新购物车")
    @PutMapping("/{id}")
    CommonResult<Cart> updatecart(@PathVariable String id, @RequestBody Cart cart) {
        cart.setCartId(id);
        return CommonResult.success(cartService.saveCart(cart));
    }

    @ApiOperation("获取全部购物车")
    @GetMapping("/all")
    CommonResult<List<Cart>> getAll() {
        return cartService.getAll();
    }

    @ApiOperation("分页获取购物车")
    @GetMapping("/allbypage")
    CommonResult<CommonPage<Cart>> getAllByPage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        return CommonResult.success(cartService.getAllByPage(currentPage, pageSize));
    }

}
